package com.tuotu.gps.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.tuotu.gps.R;
import com.tuotu.gps.RecordActivity2;
import com.tuotu.gps.UploadActivity2;
import com.tuotu.gps.entity.LocationInfo;
import com.tuotu.gps.entity.MessageEvent;
import com.tuotu.gps.utils.CollectionFile;
import com.tuotu.gps.utils.CollectionUploadResult;
import com.tuotu.gps.utils.LogUtil;
import com.tuotu.gps.utils.Utils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

/**
 * Created by XuYeWen on 2018/1/30.
 */

public class UPSuccessFragment extends BaseFragment implements UploadActivity2.OnSuccessResultListener {
    CollectionUploadResult collectionUploadResult;
    private Vector<LocationInfo> list=new Vector<>();
    private ListView listView;
    private MyArrarAdapter adapter;
    private Button btDelete;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_success, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
        listView= (ListView) view.findViewById(R.id.upSuccessListview);
        btDelete= (Button) view.findViewById(R.id.bt_delete_upload);
        ((UploadActivity2)getActivity()).setOnSuccessResultListener(this);
        collectionUploadResult=new CollectionUploadResult(RecordActivity2.GPS_UPLOAD_DIR+"success/",true);
        btDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Iterator<LocationInfo> it=list.iterator();
                        while (it.hasNext()){
                            LocationInfo locationInfo=it.next();
                            String tmpName = locationInfo.getMp3PathName();//获取全名,如fire.mp3
                            int index = tmpName.lastIndexOf(".");//"."最后出现的位置
                            String name=tmpName.substring(0,index);
                            Utils.deleteFile(name);
                            it.remove();
                        }
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                LogUtil.toast(getActivity(),"已经删除上传成功文件");
                                if(null!=adapter)
                                    adapter.notifyDataSetChanged();
                            }
                        });
                    }
                }).start();
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MainRun(MessageEvent event){
        if(event.what==MessageEvent.MSG_UPLOAD_SUCCESS_RESULT_DONE){
//            list=collectionUploadResult.getList();
            list.addAll(collectionUploadResult.getList());
            LogUtil.print("success list.size===="+list.size());
            if(null==adapter && null!=list && list.size()>0){
                adapter=new MyArrarAdapter(getActivity(),R.layout.layout_list_item,list);
                listView.setAdapter(adapter);
            }else {
                adapter=null;
                adapter = new MyArrarAdapter(getActivity(), R.layout.layout_list_item, list);
                listView.setAdapter(adapter);
//                adapter.notifyDataSetChanged();
            }
            ((UploadActivity2)getActivity()).setTitles(1,list.size());
        }else if(event.what==MessageEvent.MSG_UPLOAD_FILE_DONE){
//            collectionUploadResult=new CollectionUploadResult(RecordActivity2.GPS_UPLOAD_DIR+"success/",true);
        }
    }

    @Override
    public void onActivitySuccess(LocationInfo locationInfo) {
//        Log.e("xyww","UPSuccessFragment onActivitySuccess");
            list.add(locationInfo);
        if(null==adapter){
            adapter=new MyArrarAdapter(getActivity(),R.layout.layout_list_item,list);
            listView.setAdapter(adapter);
        }
        adapter.notifyDataSetChanged();
        ((UploadActivity2)getActivity()).setTitles(1,list.size());
    }

    class MyArrarAdapter extends ArrayAdapter {
        private Vector<LocationInfo> lists;

        public MyArrarAdapter(Context context, int resource, Vector<LocationInfo> lists) {
            super(context, resource, lists);
            this.lists=lists;
        }

        public void setLists(Vector<LocationInfo> lists) {
            this.lists = lists;
        }

        @Nullable
        @Override
        public String getItem(int position) {
            return lists.get(position).getMp3Name();
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View view=super.getView(position, convertView, parent);
            TextView tv=view.findViewById(android.R.id.text1);
            String name=getItem(position);
            tv.setText(name.substring(0,name.length()-4));
            return view;
        }
    }
}
